package server.qiang.database;

import java.sql.*;


public class database {


	public Connection connect() throws SQLException {

		try {
			Class.forName("com.mysql.jdbc.Driver");     //加载MYSQL JDBC驱动程序
			//Class.forName("org.gjt.mm.mysql.Driver");
			//System.out.println("Success loading Mysql Driver!");
		} catch (Exception e) {
			System.out.print("Error loading Mysql Driver!");

		}
		Connection connect = DriverManager.getConnection(
				"jdbc:mysql://localhost:3306/database?characterEncoding=utf8", "root", "");
		return connect;
	}


	public boolean IsUser(String id) throws SQLException

	{
		Connection connect = connect();
		Statement stmt = connect.createStatement();
		String sql = "select id from user";
		ResultSet rs = stmt.executeQuery(sql);
		int count = 0;
		while (rs.next()) {


			if (rs.getString("id").equals(id)) {
				count++;
			}
		}
		if (count == 0) {
			return false;
		} else {

			return true;

		}

	}

	public void InsertUser(String id, String email, String password) throws SQLException {
		Connection connect = connect();
		Statement stmt = connect.createStatement();
		String sql = "insert into user(id,email,emailpword) values('" + id + "','" + email + "','" + password + "')";
		stmt.executeUpdate(sql);
		sql = "select * from user";
		ResultSet rs = stmt.executeQuery(sql);
		System.out.println("插入数据库结果为：");
		while (rs.next()) {
			System.out.println(rs.getString("id"));
			System.out.println(rs.getString("email"));
			System.out.println(rs.getString("emailpword"));
		}

	}


	public String getemail(String id) throws SQLException {
		Connection connect = connect();
		Statement stmt = connect.createStatement();
		String sql = "select email from user where id ='" + id + "'";
		ResultSet rs = stmt.executeQuery(sql);
		System.out.println("get email");
		while (rs.next()) {
			String a = rs.getString("email");
			System.out.println(a);
			return a;

		}
		return null;

	}

	public String getemailPassword(String id) throws SQLException {
		Connection connect = connect();
		Statement stmt = connect.createStatement();
		String sql = "select emailpword from user where id ='" + id + "'";
		ResultSet rs = stmt.executeQuery(sql);
		System.out.println("get emailpassword");
		while (rs.next()) {
			String a = rs.getString("emailpword");
			System.out.println(a);
			return a;
		}
		return null;
	}


	public void UpdateIP(String id, String ip, String port) throws SQLException {
		Connection connect = connect();
		Statement stmt = connect.createStatement();
		String sql = "update  ipport set id='" + id + "',ip='" + ip + "',port='" + port + "'  where id ='" + id + "'";
		stmt.executeUpdate(sql);
		sql = "select * from ipport";
		ResultSet rs = stmt.executeQuery(sql);

		while (rs.next()) {
			System.out.println(rs.getString("id"));
			System.out.println(rs.getString("ip"));
			System.out.println(rs.getString("port"));
		}

	}

	public void InsertIP(String id, String ip, String port) throws SQLException {
		Connection connect = connect();
		Statement stmt = connect.createStatement();
		String sql = "insert into ipport(id,ip,port) values('" + id + "','" + ip + "','" + port + "')";
		stmt.executeUpdate(sql);
		sql = "select * from ipport";
		ResultSet rs = stmt.executeQuery(sql);
		while (rs.next()) {
			System.out.println(rs.getString("id"));
			System.out.println(rs.getString("ip"));
			System.out.println(rs.getString("port"));
		}

	}

	public String getip(String id) throws SQLException {
		Connection connect = connect();
		Statement stmt = connect.createStatement();
		String sql = "select ip from ipport where id ='" + id + "'";
		ResultSet rs = stmt.executeQuery(sql);
		System.out.println("get ip");
		while (rs.next()) {
			String a = rs.getString("ip");
			System.out.println(a);
			return a;

		}
		return null;
	}

	public String getport(String id) throws SQLException {
		Connection connect = connect();
		Statement stmt = connect.createStatement();
		String sql = "select port from ipport where id ='" + id + "'";
		ResultSet rs = stmt.executeQuery(sql);
		System.out.println("get port");
		while (rs.next()) {
			String a = rs.getString("port");
			System.out.println(a);
			return a;

		}
		return null;
	}


}
